package myJlite.dao;

import java.sql.SQLException;
import java.util.List;

import jvlite.dao.JliteORM.BeanListHandler;
import jvlite.dao.JliteORM.JliteJdbcSupport;
import jvlite.mvc.annotaion.Part;
import myJlite.entity.ProfitVO;
import myJlite.entity.StaffVO;
@Part
public class ChartsDAO extends JliteJdbcSupport implements IChartsDAO {
	@SuppressWarnings("unchecked")
	public List<StaffVO> readDeptSlr(Integer dept_id) throws SQLException{
		String sqlStr = "SELECT st.unm,slr.val FROM salary slr, staff st WHERE st.st_id = slr.st_id AND slr.st_id IN ( SELECT s.st_id FROM staff s WHERE s.dept_id = ?)";
//		String sqlStr = "SELECT st.unm, st.pwd FROM staff st WHERE dept_id = ?";
		Object[] params = { dept_id };
		return (List<StaffVO>)query(sqlStr, params, new BeanListHandler(StaffVO.class));		
	}
	
	@SuppressWarnings("unchecked")
	public List<ProfitVO> readProfitByDeptId(Integer dept_id) throws SQLException{
		String sqlStr = "SELECT val,time FROM profit WHERE dept_id=? ORDER BY time";
//		String sqlStr = "SELECT st.unm, st.pwd FROM staff st WHERE dept_id = ?";
		Object[] params = { dept_id };
		return (List<ProfitVO>)query(sqlStr, params, new BeanListHandler(ProfitVO.class));		
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<StaffVO> login(StaffVO staffVO) throws SQLException{
//		String sqlStr = "SELECT count(st.st_id) FROM staff st WHERE st.unm=? AND st.pwd=?";
		String sqlStr = "SELECT st.st_id, st.unm FROM staff st WHERE st.unm=? AND st.pwd=?";
		Object[]params = {staffVO.getUnm(), staffVO.getPwd()};
		return (List<StaffVO>)query(sqlStr, params, new BeanListHandler(StaffVO.class));
	}
/*	public static void main(String[] args) throws SQLException {
		Random rdm = new Random();
		String sql = "insert into profit(dept_id,val,time) values(?,?,?)";
		for (int i = 1; i <= 12; i++) {
			Object[]params = {2, rdm.nextDouble()*100000, "2016-"+i+"-01"};
			update(sql, params);
		}
	}*/
}
